package project1;

import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
/**
 *
 * @author ngochip
 */
public class Func {
        public Boolean isOnline() {
        try {
            Process process = java.lang.Runtime.getRuntime().exec("ping -c 1 ngochip.net");
            int returnVal = process.waitFor();
            boolean reachable = (returnVal==0);
            return reachable;
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
        }
        
        public String loadURLServer(){
            Properties prop = new Properties();
            InputStream input = null;
            String ret="";
            try {
                    input = new FileInputStream("config.properties");
                    prop.load(input);
                    ret = prop.getProperty("URLServer");
            } catch (IOException ex) {
                    System.out.println("loi load Url server1");
            } finally {
                    if (input != null) {
                            try {
                                    input.close();
                            } catch (IOException e) {
                                    System.out.println("loi load Url server2");
                            }
                    }
            }
            return ret;
        }
        public String sendGet(String surl){
		URL url;
                HttpURLConnection conn;
                BufferedReader rd;
                String line;
                String result = "";
                try {
                   url = new URL(surl);
                   conn = (HttpURLConnection) url.openConnection();
                   conn.setRequestMethod("GET");
                   rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                   while ((line = rd.readLine()) != null) {
                      result += line;
                   }
                   rd.close();
                } catch (IOException e) {
                    System.out.println("loi get");
                } catch (Exception e) {
                    System.out.println("loi get2");
                }
                return result;
	}
        public String sendPost(String url,String data){
           	URL obj;
                HttpURLConnection con;
                StringBuffer response= new StringBuffer();
            try {
                obj = new URL(url);
                con = (HttpURLConnection) obj.openConnection();
                con.setRequestMethod("POST");
                con.setDoOutput(true);
		DataOutputStream wr = new DataOutputStream(con.getOutputStream());
		wr.writeBytes(data);
		wr.flush();
		wr.close();
 
                BufferedReader in = new BufferedReader(
		        new InputStreamReader(con.getInputStream()));
		String inputLine;
		response = new StringBuffer();
 
		while ((inputLine = in.readLine()) != null) {
			response.append(inputLine);
		}
		in.close();
 
            } catch (MalformedURLException ex) {
                Logger.getLogger(Func.class.getName()).log(Level.SEVERE, null, ex);
            }
             catch (IOException e) {
                    System.out.println("loi get");
                }

	return response.toString();
        }
}
